home *** CD-ROM | disk | FTP | other *** search
- /* sf_stdio.c
- *
- * Chris Borton, 27 February 1988
- * some functions taken/adapted from BuggyEdit.c demo with LS C
- * Provides a SFFile interface to reset stdin/stdout streams
- */
-
- #include <MacTypes.h>
- #include <FileMgr.h>
- #include <StdFilePkg.h>
- #include <pascal.h>
- #include <proto.h>
-
- #include <stdio.h>
- #include "sf_stdio.h"
-
- /* globals used in this file */
- static Point SFGwhere = { 90, 82 }; /* these shouldn't be hard-coded… */
- static Point SFPwhere = { 106, 104 };
- static SFReply reply;
-
- /* copies a pascal string from p1 to p2, like strcpy() */
- void pStrCpy(char *p2, char *p1 );
- void pStrCpy( p2, p1 )
- register char *p2, *p1;
- {
- register int len;
-
- len = *p2++ = *p1++;
- while (--len >= 0)
- *p2++=*p1++;
- }
-
- /* use StdGetFile to choose text file and return with vRef & file name */
- Boolean ChooseFile( Str255 *fn, int *vRef, Str255 msg);
- static Boolean ChooseFile( fn, vRef, msg)
- Str255 *fn;
- int *vRef;
- Str255 msg;
- {
- SFTypeList myTypes;
-
- myTypes[0]='TEXT';
- SFGetFile( SFGwhere, msg, 0L, 1, myTypes, 0L, &reply );
- if (reply.good) {
- pStrCpy( (char *)fn , (char *)reply.fName);
- *vRef = reply.vRefNum;
- return(TRUE);
- }
- else return(FALSE);
- }
-
- /* get name and directory to save a new file in */
- Boolean PutFile( Str255 *fn, int *vRef, Str255 msg);
- static Boolean PutFile( fn, vRef, msg)
- Str255 *fn;
- int *vRef;
- Str255 msg;
- {
- SFPutFile(SFPwhere, msg, fn, 0L, &reply);
- if (reply.good) {
- pStrCpy( (char *) fn, (char *) reply.fName);
- *vRef = reply.vRefNum;
- return(TRUE);
- }
- else return(FALSE);
- }
-
-
- /* sf_stdin()
- *
- * reset stdin stream to read from a text file, chosen with SFGetFile
- */
- Boolean sf_stdin()
- {
- Str255 message, sf_stdin_name;
- int vRef; /* just a dummy arg here */
-
- pStrCpy((char *)&message, "\pInput file for stdin:"); /* this doesn't get shown */
- if (ChooseFile(&sf_stdin_name, &vRef, message)) {
- PtoCstr((char *)(&sf_stdin_name));
-
- if (freopen ((char *)&sf_stdin_name, "r", stdin) == NULL) {
- SysBeep(10); /* error somewhere! */
- fprintf (stderr, "can't open %s\n", sf_stdin_name);
- return FALSE;
- }
- else
- return TRUE;
-
- }
- else {
- PtoCstr((char *)(&sf_stdin_name));
- fprintf(stderr,"Cancel button hit, filename = \"%s\"\n",sf_stdin_name);
- return FALSE;
- }
- }
-
- /* sf_stdout()
- *
- * reset stdout stream to read from a text file, chosen with SFPutFile
- */
- Boolean sf_stdout()
- {
- Str255 message, sf_stdout_name;
- int vRef; /* just a dummy arg here */
-
- pStrCpy((char *)(&message), "\pOutput file for stdout:");
- pStrCpy((char *)(&sf_stdout_name), "\psf_stdout_file");
-
- if (PutFile(&sf_stdout_name, &vRef, message)) {
- PtoCstr((char *)(&sf_stdout_name));
-
- if (freopen ((char *)&sf_stdout_name, "w", stdout) == NULL) {
- SysBeep(10); /* error somewhere! */
- fprintf (stderr, "Can't open file \"%s\", errno = %d\n", sf_stdout_name,errno);
- return FALSE;
- }
- else
- return TRUE;
-
- }
- else {
- PtoCstr((char *)(&sf_stdout_name));
- fprintf(stderr,"Cancel button hit, filename = \"%s\"\n",sf_stdout_name);
- return FALSE;
- }
- }
-